/**
 * 
 */
package com.dalonedrau.jogl.opengl.ui;

import java.awt.Dimension;

import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.engine.Sprite;
import com.dalonedrau.jogl.opengl.GameFont;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;

/**
 * @author Donald
 *
 */
public final class SimpleScrollingTextWindow extends GuiTextItemImpl {
	/** the current line at which the text stops. */
	private int currentLine;
	/** a flag indicating all text has been read. */
	private boolean allTextRead;
	/** the maximum number of lines of text displayed. */
	private int numLinesDisplayed;
	/**
	 * Gets the the current line at which the text stops. 
	 * @return <code>int</code>
	 */
	public int getCurrentLine() {
		return currentLine;
	}
	/**
	 * Gets the maximum number of lines of text displayed.
	 * @return <code>int</code>
	 */
	public int getNumLinesDisplayed() {
		return numLinesDisplayed;
	}
	/**
	 * Gets the height of the text window.
	 * @return <code>int</code>
	 */
	public int getTextWindowWidth() {
		int fontId = super.getFontRefId();
		int w = 0;
		try {
			w = UIFactory.getInstance().getFontCharacterMetrics(fontId).width;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
		w *= numCharsPerLine;
		return w;
	}
	/**
	 * Gets the height of the text window.
	 * @return <code>int</code>
	 */
	public int getTextWindowHeight() {
		int fontId = super.getFontRefId();
		int h = 0;
		try {
			h = UIFactory.getInstance().getFontCharacterMetrics(fontId).height;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
		h *= numLinesDisplayed;
		return h;
	}
	/** the maximum number of characters on each line of text. */
	private int numCharsPerLine;
	/**
	 * Creates a new instance of {@link SimpleScrollingTextWindow}.
	 * @param font the font used
	 * @param newText the text to display
	 */
	private SimpleScrollingTextWindow(final String font, final String newText) {
		super(font, newText);
	}
	public SimpleScrollingTextWindow(final String font, final String newText, 
			final int maxLines, final int maxChars, final boolean debug) {
		this(font, newText);
		try {
			int w = UIFactory.getInstance().getFontCharacterMetrics(font).width;
			super.setPreferredWidth(w * maxChars);
			numCharsPerLine = maxChars;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
		numLinesDisplayed = maxLines;
		super.setDebug(debug);
	}
	public SimpleScrollingTextWindow(final String font, final String newText, 
			final int maxLines, final int maxChars) {
		this(font, newText, maxLines, maxChars, false);
	}
	/** the <code>Button</code>'s preferred size. */
	// private Dimension			preferredSize		= new Dimension(0, 0);
	/**
	 * Updates the text displayed.
	 * @param text the new text
	 */
	public void updateText(final String text) {
		PooledStringBuilder sb = 
			StringBuilderPool.getInstance().getStringBuilder();
		sb.append(super.getText());
		sb.append(text);
		super.setText(sb.toString());
		System.out.println("sstw.updatetext("+sb.toString());
		this.currentLine = super.getTextGroup().getNumberOfLines() - 1;
		sb.returnToPool();
	}
	/**
	 * {@inheritDoc}
	 *//*
	@Override
	public Dimension getPreferredSize() {
		/*
		if (preferredSize.width == 0
				|| preferredSize.height == 0) {
			// ***************************************
			// ** CALCULATE THE SIZE OF THE UI WIDGET
			// ***************************************
			int pWidth = getPreferredWidth();
			// remember if the preferred width was ever set
			boolean prefWidthSet = true;
			if (pWidth == 0) {
				pWidth = GlobalConsts.SCREEN_WIDTH;
				prefWidthSet = false;
			}
			int w = 0, h = 0;
			// add border insets to width and height
			if (super.getBorder() != null) {
				w = super.getBorder().getInsets().left;
				w += super.getBorder().getInsets().right;
				h = super.getBorder().getInsets().top;
				h += super.getBorder().getInsets().bottom;
				pWidth -= super.getBorder().getInsets().left;
				pWidth -= super.getBorder().getInsets().right;
			}
			// remove insets from preferred width before sizing text
			pWidth -= super.getInsets().left;
			pWidth -= super.getInsets().right;
			// add label insets to width and height
			w += super.getInsets().left + super.getInsets().right;
			h += super.getInsets().top + super.getInsets().bottom;
			if (super.getSprite() != null) {
				// if this is an image label,
				// add image size to label width and height
				w += super.getSprite().getWidth();
				h += super.getSprite().getHeight();
			} else if (super.getTextGroup() != null
					&& super.getText().length() > 0) {
				// this is a text label
				try {
					// ***************************************
					// ** ADD TEXT TO THE SPRITE GROUP
					// ***************************************
					sizeText();
					if (super.getTextGroup() != null
							&& super.getText() != null
							&& super.getText().length() > 0) {
						Sprite.getInstance().computeFullStats(super.getTextGroup());
					}
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
				w += getTextWidth();
				h += getTextHeight();
			}
			pWidth = w;
			if (super.getBorder() != null) {
				pWidth -= super.getBorder().getInsets().left;
				pWidth -= super.getBorder().getInsets().right;
			}
			pWidth -= super.getInsets().left;
			pWidth -= super.getInsets().right;
			if (prefWidthSet) {
				w = getPreferredWidth();
			}
			if (minimumHeight > 0) {
				h = Math.max(h, minimumHeight);
			}
			if (maximumHeight > 0) {
				h = Math.min(h, maximumHeight);
			}
			if (minimumWidth > 0) {
				w = Math.max(w, minimumWidth);
			}
			if (maximumWidth > 0) {
				w = Math.min(w, maximumWidth);
			}
			preferredSize.width = w;
			preferredSize.height = h;
			if (super.getBorder() != null) {
				super.getBorder().setPreferredSize(w, h);
			}
			try {
				// ***************************************
				// ** POSITION ALL TEXT
				// ***************************************
				if (super.getPosition() != null) {
					if (super.getBorder() != null) {
						super.getBorder().setPosition(
								super.getPosition().getX(),
								super.getPosition().getY(),
								super.getPosition().getZ()
								);
					}
					if (super.getSprite() != null) {
						double x = super.getPosition().getX(), y = super
								.getPosition().getY(), z = super.getPosition()
								.getZ();
						if (super.getBorder() != null) {
							x += super.getBorder().getInsets().left;
							y += super.getBorder().getInsets().bottom;
						}
						x += super.getInsets().left;
						y += super.getInsets().bottom;
						super.getSprite().setPosition(x, y, z);
					}
				}
				positionText();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		return preferredSize;
		*//*
	}*/
	/**
	 * Sets the currentLine
	 * @param currentLine the currentLine to set
	 */
	public void setCurrentLine(int val) {
		this.currentLine = val;
		super.getTextGroup().clearFloatStore();
	}
}
